package com.unism.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {

	public static String codedPassword(String password) {
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			try {
				digest.update(password.getBytes("utf-8"));
			} catch (UnsupportedEncodingException e) {
				e.getStackTrace();
			}
			return encodeHex(digest.digest());
		} catch (NoSuchAlgorithmException nsae) {
			nsae.getStackTrace();
		}

		return "";
	}

	private static final String encodeHex(byte[] bytes) {
		StringBuffer buf = new StringBuffer(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++) {
			if ((bytes[i] & 0xFF) < 16)
				buf.append("0");
			buf.append(Long.toString(bytes[i] & 0xFF, 16));
		}

		return buf.toString();
	}
}
